package edu.tms.dao;

import java.util.List;
import java.util.Map;

import edu.tms.bean.AssessmentInfo;
import edu.tms.bean.Evaluating;
import edu.tms.crud.JdbcUtil;
import edu.tms.dao.common.CommnonDao;

/**
 * 评测信息表Dao
 * @author Administrator
 *
 */
@SuppressWarnings("rawtypes")
public class AssessmentInfoDaoImp implements CommnonDao{
	@Override
	public long save(Object value) throws Exception {
		String sql=" INSERT INTO t_assessment_info VALUE(null,(SELECT course_id FROM t_course WHERE course_name=?), ";
				sql+="(SELECT class_id FROM t_class_info WHERE class_name=?),";
				sql+= " (SELECT tch_id FROM t_teacher WHERE tch_name=?), ";
				sql+= " ?,?,?,1,1) ";
		if(value instanceof AssessmentInfo){
			AssessmentInfo As = (AssessmentInfo)value;
			return JdbcUtil.insertAuto(sql,
					As.getCourseName(),
					As.getClassName(),
					As.getTchName(),
					As.getCreateTime(),
					As.getAssessmentStartTime(),
					As.getAssessmentEndTime()
					);
		}
		return 0;
	}

	@Override
	public int update(Object value) throws Exception {
		String sql="update t_assessment_info "
				+ "set course_id=?,tch_id=?,create_time=?,assessment_start_time=?,assessment_end_time=?,"
				+ "assessment_fulfill_state=?,assessment_state=? where assessment_id=?";
		if(value instanceof AssessmentInfo){
			AssessmentInfo As = (AssessmentInfo)value;
			JdbcUtil.update(sql,
					As.getCourseId(),
					As.getTchId(),
					As.getCreateTime(),
					As.getAssessmentStartTime(),
					As.getAssessmentEndTime(),
					As.getAssessmentFulfillState(),
					As.getAssessmentState(),
					As.getAssessmentId()
					);
		}
		return 0;
	}
	/**
	 * 删除内容
	 * 课程表 t_course，教师表t_teacher
	 * @param value
	 * @throws Exception
	 */
	@Override
	public int delete(Object value) throws Exception {
		if(value instanceof Evaluating){
			Evaluating el = (Evaluating)value;
			String sql="delete  t_assessment_info where assessment_id=?";
			return JdbcUtil.update(sql,el.getEvaluatingId());
		}
		return 0;
	}
	
	
	/**
	 * 查询最新的单条评测信息
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<AssessmentInfo> findAll(int pageNo, int pageSize) throws Exception {
		String sql=" SELECT assessment_id,c.course_id,c.course_name,t.tch_id,t.tch_name,ai.create_time,assessment_start_time,assessment_end_time,assessment_fulfill_state,assessment_state ";
				sql+=" FROM t_assessment_info AS ai ,t_course AS c, t_teacher AS t ";
				sql+=" WHERE ai.course_id =c.course_id AND ai.tch_id=t.tch_id ";
				sql+=" ORDER BY assessment_id DESC LIMIT ?,? ";
		return JdbcUtil.query(AssessmentInfo.class,sql,(pageNo-1)*pageSize,pageSize);
	}
	
	/**
	 * 查询最新的单条评测信息
	 */
	public AssessmentInfo findAssess(int stuId) throws Exception {
		String sql = "SELECT assessment_id,c.course_id,c.course_name,t.tch_id,t.tch_name,ai.create_time,"
				+ "assessment_start_time,assessment_end_time,assessment_fulfill_state,assessment_state "
				+ "FROM t_assessment_info AS ai ,t_course AS c, t_teacher AS t, t_class_info AS ci,t_student s "
				+ "WHERE ai.course_id =c.course_id AND ai.tch_id=t.tch_id "
				+ "AND ci.class_name = s.stu_class_name AND ci.`class_id` = ai.`class_id` "
				+ "AND s.stu_id = ? ORDER BY assessment_id DESC LIMIT 0,1";
		return (AssessmentInfo) JdbcUtil.uniqueBean(AssessmentInfo.class,sql,stuId);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<AssessmentInfo> findAll(Object Key,int pageNo, int pageSize) throws Exception{
		String sql="select * from t_assessment_info as ai ,t_course as c,t_teacher as t "
				+ "where ai.course_id =c.course_id and ai.tch_id=t.tch_id"
				+ " or ai.course_id=? or ai.tch_id=? or ai.create_time=? or "
				+ "ai.assessment_fulfill_state=? or ai.assessment_state=? limit ?,?";
		return JdbcUtil.query(AssessmentInfo.class,sql,Key,Key,Key,Key,Key,(pageNo-1)*pageSize,pageSize);
	}

	/**
	 * String 对应  数据库 列名（comment)
	 * Object 对应相应列名值
	 * 返回的是所查找的类中的数据。方便EXCEL导出
	 */
	@Override
	public List<Map<String,Object>> getMapList() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object findByKey(Object key) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 查询所有可查询的班级 教师 课程等
	 * @throws Exception 
	 */
	@SuppressWarnings("unchecked")
	public List<AssessmentInfo> findassement(String key) throws Exception{
		String sql=" SELECT tc.course_name,tci.class_name,tch.tch_name,asi.create_time,asi.assessment_start_time,asi.assessment_end_time,assessment_fulfill_state,asi.assessment_state ";
				sql+=" FROM t_assessment_info asi,t_teacher tch,t_class_info tci,t_course tc ";
				sql+=" WHERE asi.tch_id=tch.tch_id AND asi.class_id=tci.class_id AND asi.course_id =tc.course_id AND tc.course_name=? OR tch.tch_name=? OR tci.class_name=? ";
		return JdbcUtil.query(AssessmentInfo.class, sql,key,key,key);
	}
}
